package algotithm.leetcode.twopoint.test917;

/**
 * @author 25068
 * @Date 2021/4/27 7:56
 * @Title 917. 仅仅反转字母
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "a-bC-dEf-ghIj";
        String res = solution.reverseOnlyLetters(s);
        System.out.println(res);
    }

    public String reverseOnlyLetters(String S) {
        char[] cs = S.toCharArray();
        int left=0, right=cs.length-1;
        while(left<right){
            while(left<right && !Character.isLetter(cs[left])) {
                left++;
            }
            while(left<right && !Character.isLetter(cs[right])) {
                right--;
            }
            char tmp = cs[left];
            cs[left] = cs[right];
            cs[right] = tmp;
            left++;
            right--;
        }
        return String.valueOf(cs);
    }
}
